;var PLATFORM = PLATFORM || {};

// popup显示
PLATFORM.popupShow = function(dom){
	$(dom).show();
};
// popup隐藏
PLATFORM.popupClose = function(){
	$(".u-popup .btn-close").click(function(){
		$(this).parents(".u-popup").hide()
	});
};
// 顶部错误提示框
PLATFORM.headTip = function(msg,color){
	var headtip = $(".u-headTip");
	if(arguments.length==2){
	    headtip.find("p").css({
	        'background-color':color
	    });
	}else{
	    headtip.find("p").css({
            'background-color':"#b54449"
        });
	}
    headtip.find("p").html(msg);
    headtip.show();
    var timer = setTimeout(function(){
        headtip.hide();
        clearTimeout(timer);
        timer = null;
    }, 2000);
};
// tab
PLATFORM.tab = function(){
	$(".u-tabs .tab").click(function(){
		var $this = $(this),
			parent = $this.parents(".u-tabs"),
			tabs = parent.find(".tab-box .tab"),
			cnts = parent.find(".cnt-box .cnt"),
			idx = tabs.index($this);
		
		tabs.removeClass("sel");
		$this.addClass("sel");
		cnts.hide();
		cnts.eq(idx).show();
	});
};
// 日期选择 年-月-日
PLATFORM.datepickerRange = function(from, to){
    $(from).datepicker({
        dateFormat: "yy-mm-dd"
        /*onClose: function(selectedDate){
            $(to).datepicker("option", "minDate", selectedDate);
        }*/
    });
    $(to).datepicker({
        dateFormat: "yy-mm-dd"
        /*onClose: function(selectedDate){
            $(from).datepicker("option", "maxDate", selectedDate);
        }*/
    });
};
//在这里进行日期格式化操作
Date.prototype.Format = function (fmt) { //author: meizz 
    var o = {
        "M+": this.getMonth() + 1, //月份 
        "d+": this.getDate(), //日 
        "h+": this.getHours(), //小时 
        "m+": this.getMinutes(), //分 
        "s+": this.getSeconds(), //秒 
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
        "S": this.getMilliseconds() //毫秒 
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}
// 日期选择 年-月-日，具有初始值
PLATFORM.datepickerRangeWithDefault = function(from, to, startdate, enddate ){
    $(from).datepicker({
        dateFormat: "yy-mm-dd",
        defaultDate:startdate
        /*onClose: function(selectedDate){
            $(to).datepicker("option", "minDate", selectedDate);
        }*/
    });
    $(to).datepicker({
        dateFormat: "yy-mm-dd",
        defaultDate:enddate
        /*onClose: function(selectedDate){
            $(from).datepicker("option", "maxDate", selectedDate);
        }*/
    });
};

// 日期选择 年-月-日 时:分:秒
PLATFORM.timepickerRange = function(from, to){
    $(from).datetimepicker({
        showSecond: true,
        timeFormat: 'hh:mm:ss',
        onClose: function(selectedDate){
            $(to).datepicker("option", "minDate", selectedDate);
        }
    });
    $(to).datetimepicker({
        showSecond: true,
        timeFormat: 'hh:mm:ss',
        onClose: function(selectedDate){
            $(from).datepicker("option", "maxDate", selectedDate);
        }
    });
};

PLATFORM.datepickerRangeLimit = function(from, to, limit){
    var picker_from = $(from),
        picker_to = $(to);
    picker_from.datepicker({
        dateFormat: "yy-mm-dd",
        maxDate: PLATFORM.getDay(),
        onClose: function(selectedDate){
            var selected = new Date(selectedDate),
                today = PLATFORM.getDay();
                
            selected.setDate(selected.getDate() + limit);
            selected = PLATFORM.getDay(selected);
            
            picker_to.datepicker("option", {
                "minDate": selectedDate,
                "maxDate": selected > today ? today : selected
            });
        }
    });
    picker_to.datepicker({
        dateFormat: "yy-mm-dd",
        maxDate: PLATFORM.getDay(),
        onClose: function(selectedDate){
            var selected = new Date(selectedDate);
            selected.setDate(selected.getDate() - limit);
            
            picker_from.datepicker("option", {
                "minDate": selected,
                "maxDate": selectedDate
            });
        }
    });
};
PLATFORM.getDay = function(date){
    var now = date ? new Date(date) : new Date(),
        year = now.getFullYear(),
        month = now.getMonth(),
        day = now.getDate();
    
    month = (month + 1) > 9 ? month + 1 : "0" + String(month + 1);
    day = day > 9 ? day : "0" + String(day);
    
    return year + "-" + month + "-" + day;
}
PLATFORM.getDayLimit = function(limit, date){
    var now = date ? date : new Date();
    
    now = now.setDate(now.getDate() - limit);
        
    return PLATFORM.getDay(now);
}

$(function(){
	PLATFORM.popupClose();
	PLATFORM.tab();
});
